package team.first.dataimport;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import org.neo4j.driver.Session;
import team.first.util.DriverUtil;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first
 * @Date: 2021/12/3 11:16
 * @Description:
 */
public class StaffImport {
    public static void staffImport() {
        Session session = DriverUtil.getNewSession();
        try {
            List<Entity> staffList = Db.use().query("select * from staff");

            String addStaff = "CREATE (:Staff{" +
                    "id:$id, " +
                    "chineseName:$chineseName," +
                    "englishName:$englishName," +
                    "gender:$gender," +
                    "region:$region," +
                    "birthday:$birthday," +
                    "summary:$summary," +
                    "avatarUrl:$avatarUrl," +
                    "avatar:$avatar" +
                    "})";
            staffList.forEach(staff -> {
                Map<String, Object> params = new HashMap<>();
                params.put("id", staff.getInt("id"));
                params.put("chineseName", staff.getStr("chinese_name"));
                params.put("englishName", staff.getStr("english_name"));
                params.put("gender", staff.getStr("gender"));
                params.put("region", staff.getStr("region"));
                params.put("birthday", staff.getStr("birthday"));
                params.put("summary", staff.getStr("summary"));
                params.put("avatarUrl", staff.getStr("avatar_url"));
                params.put("avatar", staff.getStr("avatar"));
                session.run(addStaff, params);
            });
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DriverUtil.close(session);
        }

    }
}
